local fuhai = fk.CreateSkill {

  name = "joy__wfuhai",

  tags = {  },

}



fuhai:addEffect("active", {
  name = "joy__wfuhai",
  anim_type = "control",
  card_num = 0,
  target_num = 0,
  prompt = "令所有角色同时展示一张手牌，然后你选择一个方向，摸此方向上严格递增或严格递减的牌数，至少摸两张。",
  can_use = function(self, player)
    return not player:isKongcheng() and player:usedSkillTimes(fuhai.name, Player.HistoryPhase) == 0
  end,
  card_filter = Util.FalseFunc,
  on_use = function(self, room, effect)
    local player = effect.from
    room:doIndicate(player.id, table.map(room.alive_players, Util.IdMapper))
    local targets = table.filter(room.alive_players, function(p) return not p:isKongcheng() end)
    local extraData = {
      num = 1,
      min_num = 1,
      include_equip = false,
      pattern = ".",
      reason = fuhai.name,
    }
    for _, p in ipairs(targets) do
      p.request_data = json.encode({"choose_cards_skill", "所有角色同时展示一张手牌，然后"..Pg(player).."选择一个方向，摸此方向上严格递增或严格递减的牌数，至少摸两张。", false, extraData})
    end
    room:notifyMoveFocus(room.alive_players, fuhai.name)
    room:doBroadcastRequest("AskForUseActiveSkill", targets)
    for _, p in ipairs(targets) do
      local id
      if p.reply_ready then
        local replyCard = json.decode(p.client_reply).card
        id = json.decode(replyCard).subcards[1]
      else
        id = table.random(p:getCardIds("h"))
      end
      room:setPlayerMark(p, "joy__wfuhai-tmp", id)
    end
    local all_cards = {}
    for _, p in ipairs(targets) do
      if not p.dead then
        local id = p:getMark("joy__wfuhai-tmp")
        p:showCards({id})
        room:setPlayerMark(p, "@joy__wfuhai-tmp", Fk:getCardById(id).number)
        table.insertIfNeed(all_cards,id)
      end
    end
    if player.dead or #all_cards == 0 then return end
    local ch1 = "从上家开始顺时针连续"
    local ch2 = "从下家开始逆时针连续"
    local choice = room:askForChoice(player,{ch1,ch2},fuhai.name,"选择一个方向，摸等同该放向上严格单调递增或严格单调递减的牌数，至少摸两张")
    if choice == nil then
      choice = table.random({ch1,ch2})
    end 
      local ps = player
      local nonumber = false
      local n = 0
      local num = 0
      local lianxu = "no"
      if player:getMark("joy__wfuhai-tmp") ~= 0 then
        num = player:getMark("@joy__wfuhai-tmp")
        n = 1
        if num == 0 then
          nonumber = true
        end
      end
      local i = 1
      while i < #room.alive_players and not nonumber do
        i = i + 1
        for _, p in ipairs(room.alive_players) do
          if (ch1 == choice and p:getNextAlive() == ps) or (ch2 == choice and ps:getNextAlive() == p ) then
            ps = p
          end
        end
            if ps:getMark("joy__wfuhai-tmp") ~= 0 then
              local m = ps:getMark("@joy__wfuhai-tmp") 
              if lianxu == "no" then
                if num > 0 then
                  if m > num then
                    num = m
                    lianxu = "up"
                  elseif n < num and n > 0 then
                    num = m
                    lianxu = "dowm"
                  else
                    break
                  end
                else
                  num = m
                end
              elseif lianxu == "up" then
                if m > num then
                  num = m
                else
                  break
                end
              elseif lianxu == "down" then
                if m < num and n > 0 then
                  num = m
                else
                  break
                end
              end
              n = n + 1
            end
      end
      n = math.max(n,2)
      for _, p in ipairs(room.players) do
        room:setPlayerMark(p, "@joy__wfuhai-tmp",0)
        room:setPlayerMark(p, "joy__wfuhai-tmp",0)
      end
      if not player.dead then
        player:drawCards(n,fuhai.name)
      end
  end,
})

return fuhai